﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EnvironmentalMonitor.Support.Business.Environmental;
using EnvironmentalMonitor.Support.Business;
using EnvironmentalMonitor.Support.Module;
using EnvironmentalMonitor.Support.Resource;

namespace EnvironmentalMonitor.WebApplication.Environmental.DetectorType
{
    /// <summary>
    /// 保存Json数据页面类
    /// </summary>
    public partial class SaveJson : EnvironmentalMonitor.WebApplication.Abstract.AbstractSaveJson
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            const string GUID = "guid";
            const string REMARK = "remark";

            const string NAME = "name";
            const string TYPE = "type";
            const string MINIMUM_A = "minimumA";
            const string MINIMUM_B = "minimumB";
            const string MAXIMUM_A = "maximumA";
            const string MAXIMUM_B = "maximumB";
            const string THRESHOLD_A = "thresholdA";
            const string THRESHOLD_B = "thresholdB";
            const string CODE = "code";
            const string DESCRIPTION_A = "descriptionA";
            const string DESCRIPTION_B = "descriptionB";
            const string NORMAL = "normal";
            const string ERROR = "error";

            EnvironmentalMonitor.Support.Module.Manage.User sessionUser = this.Session[Constant.SESSION_KEY_USER] as EnvironmentalMonitor.Support.Module.Manage.User;
            DateTime now = DateTime.Now;

            DetectorTypeBusiness business = new DetectorTypeBusiness();

            EnvironmentalMonitor.Support.Module.Environmental.DetectorType module = new Support.Module.Environmental.DetectorType();
            module.Guid = this.ParameterString(GUID);
            module.InsertUserId = sessionUser.Guid;
            module.InsertTime = now;
            module.UpdateUserId = sessionUser.Guid;
            module.UpdateTime = now;
            module.Remark = this.ParameterString(REMARK);
            module.Validity = true;

            ushort minimumA = this.ParameterUshort(MINIMUM_A, 0);
            ushort minimumB = this.ParameterUshort(MINIMUM_B, 0);
            ushort maximumA = this.ParameterUshort(MAXIMUM_A, 0);
            ushort maximumB = this.ParameterUshort(MAXIMUM_B, 0);
            ushort thresholdA = this.ParameterUshort(THRESHOLD_A, 0);
            ushort thresholdB = this.ParameterUshort(THRESHOLD_B, 0);

            module.Name = this.ParameterString(NAME);
            module.Type = this.ParameterDetectorTypes(TYPE, DetectorTypes.Unknown);

            switch (module.Type)
            {
                case DetectorTypes.Switch:
                    module.Minimum = minimumA;
                    module.Maximum = maximumA;
                    module.Threshold = thresholdA;
                    break;
                case DetectorTypes.Area:
                    module.Minimum = minimumA;
                    module.Maximum = maximumA;
                    module.Threshold = thresholdA;
                    break;
                case DetectorTypes.DoubleArea:
                    module.Minimum = (ushort)(minimumA * 0x100 + minimumB);
                    module.Maximum = (ushort)(maximumA * 0x100 + maximumB);
                    module.Threshold = (ushort)(thresholdA * 0x100 + thresholdB);
                    break;
            }

            module.Code = this.ParameterByte(CODE, 0);
            module.DescriptionA = this.ParameterString(DESCRIPTION_A);
            module.DescriptionB = this.ParameterString(DESCRIPTION_B);

            bool guidNull = false;
            if (string.IsNullOrEmpty(module.Guid))
            {
                guidNull = true;
                module.Guid = business.GUID();
            }

            module.Normal = string.Format("Upload/DetectorType/Normal/{0}.jpg", module.Guid);
            string normalUrl = string.Format("{0}{1}", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath, module.Normal);
            this.ParameterFile(NORMAL, normalUrl);
            module.Error = string.Format("Upload/DetectorType/Error/{0}.jpg", module.Guid);
            string errorUrl = string.Format("{0}{1}", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath, module.Error);
            this.ParameterFile(ERROR, errorUrl);

            this.SaveJsonData = new Abstract.SaveJsonData();
            if (guidNull)
            {
                this.SaveJsonData.success = business.Insert(module);
            }
            else
            {
                this.SaveJsonData.success = business.Update(module);
            }
            if (!this.SaveJsonData.success)
            {
                this.SaveJsonData.msg = "数据操作失败！";
            }

            string json = this.Json();

            this.Response.Write(json);
            this.Response.Flush();
            this.Response.End();
        }
    }
}